﻿/***************************************************************************

Copyright (c) 2008 Microsoft Corporation. All rights reserved.

***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using VsxFactory.Modeling;

namespace VsxFactory.Modeling.VisualStudio
{
    /// <summary>
    /// Class to interact with the VS status bar
    /// </summary>
    public class VsStatusBar
    {
        #region Fields
        IServiceProvider _serviceProvider;
        private uint _statusBarCookie;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="StatusBar"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public VsStatusBar(IServiceProvider serviceProvider)
        {
            this._serviceProvider = serviceProvider;
        }
        #endregion

        #region Properties
        private IVsStatusbar bar;

        /// <summary>
        /// Gets the status bar.
        /// </summary>
        /// <value>The status bar.</value>
        public IVsStatusbar Bar
        {
            get
            {
                if (bar == null)
                {
                    bar = _serviceProvider.GetService(typeof(SVsStatusbar)) as  IVsStatusbar;
                }

                return bar;
            }
        }

        public void Progress( string label, int amountCompleted, int total)
        {
            Bar.Progress(ref _statusBarCookie, 1, label, (uint)amountCompleted, (uint)total);
        }

        public void StopProgress()
        {
            if( _statusBarCookie != 0)
                Bar.Progress(ref _statusBarCookie, 0, String.Empty, 0, 0);
            _statusBarCookie = 0;
        }
        #endregion

        #region Public Implementation
        /// <summary>
        /// Displays the message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void DisplayMessage(string message)
        {
            int frozen;

            Bar.IsFrozen(out frozen);

            if (frozen == 0)
            {
                Bar.SetText(message);
            }
        }
        #endregion
    }
}